package com.example.googlemapdrawroutelast;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;


public class CharacteristicPointsParser {
	
    static ArrayList<LatLng> coordinatesLatLng;
    
    public static String getJSON(List<CharacteristicPoint> points) throws JSONException
    {
    	JSONArray jsonPoints = new JSONArray();
    	for(CharacteristicPoint point : points)
    	{
    		jsonPoints.put(createJSONFromCharacteristicPoint(point));
    	}
    	return jsonPoints.toString();
    	
    }
    
    
    
    private static JSONObject createJSONFromCharacteristicPoint(
			CharacteristicPoint point) throws JSONException {
		JSONObject jsonPoint = new JSONObject();
		jsonPoint.put("namePoint", point.getNamePoint());
		jsonPoint.put("description", point.getDescription());
		jsonPoint.put("coordinate", createJSONFromLatLng(point.getCoordinate()));    	
		return jsonPoint;
	}
    
    private static JSONObject createJSONFromLatLng(LatLng coordinate) throws JSONException
    {
    	JSONObject jsonObject = new JSONObject();
    	jsonObject.put("latitude", coordinate.latitude);
    	jsonObject.put("longitude", coordinate.longitude);
    	return jsonObject;    	
    }



	public static List<CharacteristicPoint> getPointsFromString(String pointsAsJSON) throws Exception
	{		
		JSONArray array = new JSONArray(pointsAsJSON);
		List<CharacteristicPoint> points = new ArrayList<CharacteristicPoint>();
		for (int i = 0; i < array.length(); i++)
		{
			JSONObject obj = array.getJSONObject(i);
			points.add(new CharacteristicPoint(createLatLngFromJSON( obj.getJSONObject("coordinate")), obj.getString("namePoint"), obj.getString("description")));
		}
		return points;
	}



	private static LatLng createLatLngFromJSON(JSONObject jsonObject) throws JSONException {
		// TODO Auto-generated method stub
		return new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"));
	}
}